#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: BaseRepository.cs
//

#endregion

using System;
using System.Threading;
using Novagoal.Models;

namespace Novagoal.Repositories.Entities
{
    public abstract class BaseRepository
    {
        #region PROPERTIES

        protected GoalkeeperEntities EntitiesContext { get; private set; }   

        #endregion

        #region PUBLIC METHODS

        public static DateTime UniversalDate
        {
            get
            {
                var ci = Thread.CurrentThread.CurrentCulture; 
                return Convert.ToDateTime(DateTime.UtcNow, ci); 
            }
        }

        protected BaseRepository()
        {
            EntitiesContext = new GoalkeeperEntities();
        }

        #endregion

        #region PROTECTED METHODS

        protected static Message CreateMessage(messages entity)
        {
            return entity == null
                       ? null
                       : new Message
                             {
                                 Id = entity.id,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by,
                                 MessageString = entity.message,
                                 CreatedBy = CreatePerson(entity.people)
                             };
        }

        protected static Player CreatePlayer(players entity)
        {
            return entity == null
                       ? null
                       : new Player
                             {
                                 Id = entity.id,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by,
                                 Name = entity.name,
                                 Position = entity.position,
                                 Mvp = entity.mvp,
                                 //Team = CreateTeam(entity.teams)
                             };
        }

        protected static Party CreateParty(parties entity)
        {
            return entity == null
                       ? null
                       : new Party
                             {
                                 Id = entity.id,
                                 Name = entity.name,
                                 CreatedBy = CreatePerson(entity.people),
                                 Tournament = CreateTournament(entity.tournaments),
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by
                             };
        }

        protected static Result CreateResult(results entity)
        {
            return entity == null
                       ? null
                       : new Result
                             {
                                 Id = entity.id,
                                 TeamAScore = entity.team_a_score,
                                 TeamBScore = entity.team_b_score,
                                 Points = entity.points,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by,
                                 Player = CreatePlayer(entity.players)
                             };
        }

        protected static Team CreateTeam(teams entity)
        {
            return entity == null
                       ? null
                       : new Team
                             {
                                 Id = entity.id,
                                 Name = entity.name,
                                 FlagPath = entity.flag,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by
                             };
        }

        protected static Game CreateGame(games entity)
        {
            return entity == null
                       ? null
                       : new Game
                             {
                                 Id = entity.id,
                                 TeamA = CreateTeam(entity.teamsA),
                                 TeamB = CreateTeam(entity.teamsB),
                                 StartedAt = entity.started_at,
                                 Tournament = CreateTournament(entity.tournaments),
                                 Enabled = entity.enabled,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by
                             };
        }

        protected static Tournament CreateTournament(tournaments entity)
        {
            return entity == null
                       ? null
                       : new Tournament
                             {
                                 Id = entity.id,
                                 Name = entity.name,
                                 TrophyPath = entity.trophie,
                                 Enabled = entity.enabled,
                                 StartAt = entity.start_at,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by
                             };
        }

        protected static User CreateUser(users entity)
        {
            return entity == null
                       ? null
                       : new User
                             {
                                 Id = entity.id,
                                 ActivationCode = entity.activation_code,
                                 Enabled = entity.enabled,
                                 Password = entity.password,
                                 Salt = entity.salt,
                                 LastAccess = entity.last_access,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by,
                                 Person = CreatePerson(entity.people),
                                 Role = CreateRole(entity.roles)
                             };
        }

        protected static Person CreatePerson(people entity)
        {
            return entity == null
                       ? null
                       : new Person
                             {
                                 Id = entity.id,
                                 FirstName = entity.first_name,
                                 LastName = entity.last_name,
                                 EmailAddress = entity.email_address,
                                 Gender = entity.gender,
                                 Language = entity.language,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 Avatar = entity.avatar,
                                 UpdatedBy = entity.updated_by
                             };
        }

        protected static Role CreateRole(roles entity)
        {
            return entity == null
                       ? null
                       : new Role
                             {
                                 Id = entity.id,
                                 Name = entity.name,
                                 CreatedAt = entity.created_at,
                                 UpdatedAt = entity.updated_at,
                                 UpdatedBy = entity.updated_by
                             };
        }

        #endregion
    }
}